/****************** extending widget ****************/
/** DEPENDECY currently cannot be used without celebrio.js and albireo system **/

(function($) {
    var _init = $.ui.dialog.prototype._init;
    $.ui.dialog.prototype.maximize = function () {
        this.options.maximized = true;
        this._maximize();
        this._trigger("resize");
    };
	
    $.ui.dialog.prototype.minimize = function () {
        this.uiDialog.hide();
        this.options.minimized = true;
        celebrio.applications.focus = null;
    };

    $.ui.dialog.prototype.unminimize = function () {
        this.uiDialog.show();
        this.moveToTop();
        this.options.minimized = false;
    };
	
    //$("#a").click(function (event) {
    //		event.preventDefault();
    //	})
    $.ui.dialog.prototype._toggleMax = function() {
        if (this.options.maximized) {
            this._restore();
        } else {
            this._maximize();
        }
        this._trigger("resize");
    }
		
    $.ui.dialog.prototype._maximize = function() {
        var self = this;
        self.position = {
            top : self.uiDialog.css("top"),
            left : self.uiDialog.css("left"),
            width: self.uiDialog.width(),
            height: self.uiDialog.height()
            }
        self.options.width = $(window).width();//celebrio.desktop.getElement().width();
        self.options.height = celebrio.desktop.getElement().height();
        self._setOption("position", [0,0]);
        self._size();
        //alert(self.uiDialog.position().left);
        //self.uiDialog.css({width: albireo.desktop.getElement().width(), height: albireo.desktop.getElement().height()});
        self.options.maximized = true;
        self.uiDialog.find('.ui-dialog-titlebar-maxi').addClass('ui-dialog-titlebar-restore').removeClass('ui-dialog-titlebar-maxi');
    //self.uiDialog.find('.dialogApplication').focus();
    }
		
    $.ui.dialog.prototype._restore = function() {
        var self = this;
        self.options.width = self.position.width;
        self.options.height = self.position.height;
        self._setOption("position", [self.position.left,self.position.top]);
        self._size();
        //self.uiDialog.css({top:self.position.top, left:self.position.left, width: self.position.width, height: self.position.height, maxHeight: albireo.desktop.getElement().height()});
        self.options.maximized = false;
        self.uiDialog.find('.ui-dialog-titlebar-restore').addClass('ui-dialog-titlebar-maxi').removeClass('ui-dialog-titlebar-restore');
    }

    $.ui.dialog.prototype._init = function() {
        var self = this;
        _init.apply(this, arguments);
		
        if (self.options.maximized) {
            self._maximize();
        }
		
        if (self.options.minimized) {
            self.minimize();
        }
		
        if (self.options.minimizable) {
            self.uiDialog.find('.ui-dialog-titlebar-close').before(
                $('<a href="#"></a>')
                .addClass(
                    'ui-dialog-titlebar-mini ' +
                    'ui-corner-all'
                    )
                .attr({
                    'role' : 'button',
                    'unselectable' : 'on',
                    'style' : '-moz-user-select: none'
                })
                .click(function(event) {
                    self.minimize();
                    return false;
                }).html('<span class="ui-icon ui-icon-closethick" unselectable="on" style="-moz-user-select: none;">minimize</span>')
                );
        }
		
        self.uiDialog.css({
            maxHeight: celebrio.desktop.getElement().height()
            });
		
        if (self.options.maximizable) {
            self.uiDialog.find('.ui-dialog-titlebar-close').before(
                $('<a href="#"></a>')
                .addClass(
                    'ui-dialog-titlebar-maxi ' +
                    'ui-corner-all'
                    )
                .attr({
                    'role' : 'button',
                    'unselectable' : 'on',
                    'style' : '-moz-user-select: none'
                })
                .click(function(event) {
                    self._toggleMax();
                    return false;
                }).html('<span class="ui-icon ui-icon-closethick" unselectable="on" style="-moz-user-select: none;">maximize</span>')
                );
			
            self.uiDialog.find('.ui-dialog-titlebar').dblclick(function(event) {
                self._toggleMax();
            });
        }
		
        if (self.options.unclosable) {
            self.uiDialog.find('.ui-dialog-titlebar-close').remove();
        }
        if (self.options.draggable) {
            self.uiDialog.draggable({
                scroll: false,
                containment: false
            });
        }
    };

})(jQuery);
/***************** End of widget *********************/ 